package com.xgkj.diyiketang.util.xiangce;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.xgkj.diyiketang.activity.BaseActivity;
import com.xgkj.diyiketang.activity.MyPhotoActivity;
import com.xgkj.diyiketang.activity.UserInfoActivity;
import com.xgkj.diyiketang.util.zoom.PhotoView;
import com.xgkj.diyiketang.util.zoom.ViewPagerFixed;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

public class GalleryActivity extends BaseActivity {
    private final String TAG = getClass().getSimpleName();
    private Context mContext;
    private ViewPagerFixed viewPager;
    private List<View> mList;
    private GalleryAdapter mAdapter;
    private Button sendBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        mContext = this;
        initView();
    }

    private void initView(){
        viewPager = (ViewPagerFixed) findViewById(R.id.viewPager);
        sendBtn = (Button) findViewById(R.id.sendphoto);
        initListViews();
        mAdapter = new GalleryAdapter(mList);
        viewPager.setAdapter(mAdapter);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MyPhotoActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
    }


    private void initListViews() {
        if (mList == null)
            mList = new ArrayList<View>();
        for (int i=0;i<PhotoConstant.selectImageList.size();i++) {
            PhotoImageInfo info = PhotoConstant.selectImageList.get(i);
            PhotoView img = new PhotoView(mContext);
            img.setImageResource(R.drawable.zhanweitu);
            img.setImageBitmap(info.getBitmap());
            img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            mList.add(img);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            for (int i=0;i<PhotoConstant.selectImageList.size();i++){
                Bitmap bitmap = PhotoConstant.selectImageList.get(i).getBitmap();
                if ( bitmap != null && !bitmap.isRecycled()){
                    bitmap.recycle();
                    bitmap = null;
                    PhotoConstant.selectImageList.get(i).setBitmap(null);
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    class GalleryAdapter extends PagerAdapter{
        private List<View> viewList;
        public GalleryAdapter(List<View> list) {
            viewList = list;
        }

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));
        }
    }
}
